package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.constant.ProductConstants;
import com.powernode.domain.ProdTag;
import com.powernode.model.Result;
import com.powernode.service.ProdTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年07月27日 16:16
 * @Description 分组标签管理控制器
 */
@Api(tags = "分组标签接口管理")
@RequestMapping("product/prodTag")
@RestController
public class ProductTagController {

    @Autowired
    private ProdTagService productTagService;
    /*
     *分页查询多组标签
     * @author RenBoQing
     * @date 2024/7/27 18:58
     * @param current
     * @param size
     * @param title
     * @param status 
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.ProdTag>>
     */
    @ApiOperation("分页查询多组标签")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<Page<ProdTag>> loadProductTagPage(@RequestParam(defaultValue = "1") Long current,
                                                    @RequestParam(defaultValue = "10") Long size,
                                                    @RequestParam(required = false) String title,
                                                    @RequestParam(required = false) Integer status) {
        Page<ProdTag> objectPage = new Page<>(current, size);
        objectPage = productTagService.page(objectPage, new LambdaQueryWrapper<ProdTag>()
                .eq(ObjectUtil.isNotNull(status), ProdTag::getStatus, status)
                .like(ObjectUtil.isNotNull(title), ProdTag::getTitle, title)
                .orderByDesc(ProdTag::getSeq)
        );
        return Result.success(objectPage);
    }

    /*
     * 新增分组标签
     * @author RenBoQing
     * @date 2024/7/27 19:04
     * @param productTag
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增分组标签")
    @PostMapping("")
    @PreAuthorize("hasAuthority('prod:prodTag:save')")
    public Result<String> saveProductTag(@RequestBody ProdTag productTag) {
        boolean success = productTagService.saveProTag(productTag);
        return Result.handle(success);
    }
    /*
     * 根据标签Id查询标签的详情
     * @author RenBoQing
     * @date 2024/7/27 19:53
     * @param tagId
     * @return com.powernode.model.Result<com.powernode.domain.ProdTag>
     */
    @ApiOperation("根据标签Id查询标签的详情")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<ProdTag> getTagById(@PathVariable("id") Long tagId) {
        ProdTag productTag = productTagService.getById(tagId);
        return Result.success(productTag);
    }
    /*
     * 修改分组标签
     * @author RenBoQing
     * @date 2024/7/27 19:04
     * @param productTag
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改分组标签信息")
    @PutMapping("")
    @PreAuthorize("hasAuthority('prod:prodTag:update')")
    public Result<String> updateProductTag(@RequestBody ProdTag productTag) {
        boolean success = productTagService.updateProTag(productTag);
        return Result.handle(success);
    }
    /*
     * 根据id删除商品分组标签
     * @author RenBoQing
     * @date 2024/7/27 20:13
     * @param tagId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("根据id删除商品分组标签")
    @DeleteMapping
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public Result<String> deleteProductTag(@PathVariable("id") Long tagId) {
        boolean success = productTagService.removeById(tagId);
        return Result.handle(success);
    }

    @ApiOperation("查询状态正常的商品分组标签集合")
    @GetMapping("/listTagList")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<List<ProdTag>> loadProductTagList() {
        List<ProdTag> list = productTagService.queryProdTagList();
        return Result.success(list);
    }
}
